{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import os\n",
    "import subprocess\n",
    "import time\n",
    "\n",
    "import psutil\n",
    "\n",
    "logging.basicConfig(level='DEBUG', format='%(asctime)s line:%(lineno)d [%(levelname)s]- %(message)s')\n",
    "logging.debug(os.getcwd())\n",
    "\n",
    "\n",
    "def is_process_running(pid: int) -> bool:\n",
    "    try:\n",
    "        process = psutil.Process(pid)\n",
    "    except psutil.NoSuchProcess:\n",
    "        logging.error('NoSuchProcess')\n",
    "    else:\n",
    "        if process.status() == 'running':\n",
    "            return True\n",
    "        process.kill()\n",
    "        return False\n",
    "\n",
    "\n",
    "def call(cmd_):\n",
    "    p = subprocess.Popen(cmd_, shell=True)\n",
    "    if is_process_running(p.pid):\n",
    "        return p.pid\n",
    "    return 0\n",
    "\n",
    "\n",
    "def search_process_first(proc_name) -> psutil.Process:\n",
    "    for proc in psutil.process_iter():\n",
    "        try:\n",
    "            if proc_name:\n",
    "                if proc_name.lower() in proc.name().lower():\n",
    "                    return proc\n",
    "        except psutil.AccessDenied:\n",
    "            pass\n",
    "\n",
    "\n",
    "def _execuet(cmd_):\n",
    "    process_name = cmd_[0] if isinstance(cmd_, list) else cmd_.split()[0]\n",
    "    while True:\n",
    "        logging.debug('Search Process: %s', process_name)\n",
    "        if search_process_first(process_name):\n",
    "            logging.info('Running Now')\n",
    "        else:\n",
    "            logging.info('Not Running')\n",
    "            if call(cmd_):\n",
    "                logging.info('Call Success: %s', cmd_)\n",
    "            else:\n",
    "                logging.error('Fail!!! :%s', cmd_)\n",
    "        time.sleep(5)\n",
    "\n",
    "\n",
    "def protect_execute(cmd_):\n",
    "    _execuet(cmd_)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "#     cmd = 'java -jar aaa.jar'\n",
    "    cmd = 'notepad'\n",
    "    protect_execute(cmd)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
